package com.woniuxy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.conmon.MyPageData;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.pojo.After;
import com.woniuxy.pojo.dto.AfterDTO;
import com.woniuxy.service.AfterService;
import com.woniuxy.utils.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/after")
public class AfterController {

    @Autowired
    AfterService afterService;

    @GetMapping("/getAllService/{pageSize}/{pageNum}/{service}")
    public ResponseEntity<?> getAllService(@PathVariable Integer pageSize,
                                           @PathVariable Integer pageNum,
                                           @PathVariable Integer service){
        Page<After> page = afterService.getAllService(pageSize,pageNum,service);
        List<After> afters = page.getRecords();
        MyPageData data = new MyPageData(page.getTotal(),afters);
        return ResponseEntity.ok(data);
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response,
                         @RequestParam("ids") String idsStr) {
        // 将字符串转为 List<Long>
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());

        List<After> afters = afterService.getOrderByIds(ids);
        EasyExcelUtil.export(response, "订单数据", "模板", afters, After.class);
    }

    @DeleteMapping("/deleteByIds/{ids}")
    public ResponseEntity<?>deleteByIds(@PathVariable List<Long> ids){
        afterService.deleteByIds(ids);
        return ResponseEntity.ok(SystemCode.DELETESERVICE_SUCCESS.getMessage());
    }

    @PostMapping("/selectByNumAndStatus/{service}")
    public ResponseEntity<?> selectByNumAndStatus(@RequestBody AfterDTO afterDTO,
                                                  @PathVariable Integer service){
        List<After> afters = afterService.selectByNumAndStatus(afterDTO,service);
        return ResponseEntity.ok(afters);
    }
}

